package com.hdit.guimei.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdit.guimei.domain.Goods;
import com.hdit.guimei.domain.Order_item;
import com.hdit.guimei.mapper.GoodsMapper;
import com.hdit.guimei.service.GoodsService;
import com.hdit.guimei.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @基本功能:
 * @ClassName: GoodsServiceImpl
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 10:21
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper mapper;



    /***
    * @Method 根据商品名称或者商品品牌模糊查询
    * @Author 冰镇小可乐
    * @params  [map]
    * @Return java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception  
    * @Date 2021/1/19 14:07
    */
    @Override
    public List<Goods> findGoodsBy(String param,int pageNum,int pageSize) {

        int i=mapper.findGoodsByCount(param);
        System.out.println(i);
        Map<String,Object> map = PageUtil.Page(i,pageNum,pageSize);
        map.put("param",param);
        System.out.println(map);
        List<Goods> list=mapper.findGoodsBy(map);
        System.out.println(list);
        return list;
    }

    /***
    * @Method 根据商品的价格区间进行商品信息的查询
    * @Author 冰镇小可乐
    * @params  [map]
    * @Return java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception  
    * @Date 2021/1/19 14:08
    */
    @Override
    public List<Goods> findGoodsByPrice(double start,double last,int pageNum,int pageSize) {
        int i=mapper.findGoodsByPriceCount(start,last);
        Map<String,Object> map=PageUtil.Page(i,pageNum,pageSize);
        map.put("start",start);
        map.put("last",start);
        System.out.println(map);
        List<Goods> list =mapper.findGoodsByPrice(map);
        System.out.println(list);
        return list;
    }
    /**
    * @Method: addGoods
    * @函数(方法)功能: 添加商品信息
    * @Author: 李嘉明
    * @params:  [goods]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/21 19:49
    */
    @Override
    public int addGoods(Goods goods) {
        return  mapper.addGoods(goods);
    }
    /**
     * @Method: updateGoods
     * @函数(方法)功能: 更新商品信息
     * @Author: 李嘉明
     * @params:  [goods]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/21 19:50
     */
    @Override
    public int updateGoods(Goods goods) {
        return mapper.updateGoods(goods);
    }
    /**
     * @Method: findGoodsByGoodsId
     * @函数(方法)功能: 通过商品id查询商品
     * @Author: 李嘉明
     * @params:  [id]
     * @Return: com.hdit.guimei.domain.Goods
     * @Exception:
     * @Date: 2021/1/21 19:50
     */
    @Override
    public Goods findGoodsByGoodsId(int id) {
        return mapper.findGoodsByGoodsId(id);
    }
    /***
    * @Method 销量(第一版）
    * @Author 冰镇小可乐
    * @params  [param]
    * @Return java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception
    * @Date 2021/1/20 17:16
    */
    @Override
    public List<Goods> findGoodIdBy(String param) {
        //查询所需要的商品
        List<Goods> listGoods_id=mapper.findGoodsByM(param);
        System.out.println("List的长度:"+listGoods_id.size());
        for (Goods goods : listGoods_id) {
            System.out.println(goods.getGood_name());
        }
        List<Order_item> itemList=new ArrayList<Order_item>();
        List<Goods> list =new ArrayList<Goods>();
        for (Goods goods : listGoods_id) {
            //查询所需要商品的销量计数
            int count=mapper.findCountByGood_id(goods.getGood_id());
            //订单明细表实体类
            Order_item order_item=new Order_item();
            order_item.setGood_id(goods.getGood_id());
            order_item.setCount(count);
            itemList.add(order_item);
        }
        //lambda排序
        Collections.sort(itemList, Comparator.comparing(Order_item::getCount).reversed()
                .thenComparing(Order_item::getGood_id));
        for (Order_item o : itemList) {
            System.out.println(o.getGood_id());
            System.out.println(o.getCount());
            Goods goods=mapper.findGoodsByGoods_id(o.getGood_id());
            list.add(goods);
        }
        return list;
    }
    /***
    * @Method 商品销量（第二版）
    * @Author 冰镇小可乐
    * @params  [param]
    * @Return java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception
    * @Date 2021/1/20 17:19
    */
    @Override
    public List<Goods> findSalesBy(String param) {
        List<Goods> list =mapper.findSalesBy(param);
        return list;
    }
    /**
    * @Method: findAllGoods
    * @函数(方法)功能: 查询所有商品
    * @Author: 李嘉明
    * @params:  []
    * @Return: java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception:
    * @Date: 2021/1/26 21:07
    */
    @Override
    public PageInfo<Goods> findAllGoods(int pageNum,int pageSize) {
        System.out.println("serviceImpl类-->页码："+pageNum);
        System.out.println("serviceImpl类-->每页数量："+pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = mapper.findAllGoods();
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public List<Goods> findGoodsByCategory(int category_id) {

        return mapper.findGoodsByCategory(category_id);
    }

    /**
     *@methodName  findgoodsbyclass
     *@方法功能描述：根据分类查找商品
     *@params  [good_class]
     *@return  java.util.List<com.hdit.guimei.domain.Goods>
     *@author   张宝玉
     *@Date    2021/1/26 19:39
     */
    @Override
    public List<Goods> findgoodsbyclass(String good_class) {
        List<Goods> goodsList = mapper.findgoodsbyclass(good_class);
        return goodsList;
    }
    //查找图片
    @Override
    public List<Goods> findgoodpicture(int good_id) {
        return mapper.findgoodpicturebyid(good_id);
    }
}
